package org.oschina.helper.messagequeue.impl;

import org.apache.commons.lang.StringUtils;
import org.oschina.Constants;
import org.oschina.helper.JsonHelper;
import org.oschina.helper.cache.ICacheHelper;
import org.oschina.helper.messagequeue.IMessageQueueHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息队列实现类
 *  Redis
 */
//@Component
public class RedisMessageQueueHelperImpl implements IMessageQueueHelper {

	@Autowired
	private ICacheHelper cacheHelper;
	
	@Override
	public boolean send(String key, Object message)
	{
		if(StringUtils.isBlank(key))
			return false;
		
		cacheHelper.lpush(key, JsonHelper.toJson(message),Constants.QUEUE_TIMEOUT);
		return true;
	}

	@Override
	public <T> Object get(String key,Class<T> type)
	{
		if(StringUtils.isBlank(key))
			return null;
		
		return cacheHelper.rpop(key, type);
	}

}